﻿using Foxit.PDF;
using Foxit.PDF.Imaging;
using Foxit.PDF.IO;
using Foxit.PDF.PageElements;
using Sustainalytics.Entities;
using Sustainalytics.Entities.RiskRatings;
using Sustainalytics.Reporting.Framework;
using Sustainalytics.RiskRatings.Reporting.Charts;
using Sustainalytics.RiskRatings.Reporting.Headers_and_Footers;
using Sustainalytics.Utils;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sustainalytics.RiskRatings.Reporting
{
    public class MaterialIssuesOverviewSection : PdfReportSectionGenerator
    {
        private static readonly IndicatorCodeComparer _indicatorComparer = new IndicatorCodeComparer();

        public MaterialIssuesOverviewSection(int index):base(index)
        {
            this._reportSection.Name = "MEI Overview section";
        }
        public override void ComposeSectionContent()
        {
            isCoreReport = _dataSource.ReportType == ReportType.CoreRiskRatingsReport || _dataSource.ReportType == ReportType.SummaryCoreRiskRatingsReport;
            PdfReportPage page;
            if (isCoreReport)
                page = new PdfReportPage_Core(_report, _dataSource, 2);
            else
                page = new PdfReportPage_RiskRatingOverview(_report, _dataSource, false);
            _report.Pages.Add(page);
            float top = page.Top + (float)VerticalSpacing.Small;
            float currentYOffset = top;
            if (isCoreReport)
            {
                OutlinePositions.Add(new OutlinePosition { Name = "Management Details", PageNumber = OutlinePageCount, VerticalPosition = page.Top - HeaderHeight });
                AddIndicatorTable(_dataSource.CoreCompanyProfile, page, 0, currentYOffset, _report.PageMaxWidth, _report.PageMaxHeight);
            }
            else
            {
                OutlinePositions.Add(new OutlinePosition { Name = "Material Issues Overview", PageNumber = OutlinePageCount, VerticalPosition = page.Top - HeaderHeight });
                currentYOffset = MaterialIssuesOverview(page, top);
                AddRiskDetailsChart(page, currentYOffset);
            }
        }

        float MaterialIssuesOverview(PdfReportPage page, float top)
        {
            float left = 0;
            float currentYOffset = top;

            var headerFontSize = (float)TextSizes.RegularText - 1f;
            var headerYOffSet = 85f;
            var attribution_details_image = ReportHelper.GetFile("icon-attribution.png");
            page.Elements.Add(new Image(ImageData.GetImage(attribution_details_image), 0, headerYOffSet - 8f, 0.2f));
            page.Elements.Add(new Label("Attribution Details", 25, headerYOffSet - 6f, 150f, 12f, Fonts.MontserratBold, 10f, TextAlign.Left, RgbColor.Black));
            page.Elements.Add(new Line(left + 130, headerYOffSet, _report.PageMaxWidth, headerYOffSet, 1, RgbColor.Black));


            top += 15;


            const float firstRowHeight = 5f;
            const float secondRowHeight = 3f;
            const float rowHeight = 25f;
            const float totalRowHeight = 20f;

            var style = new FormattedTextAreaStyle(Fonts.RobotoFontFamily, 7f, false)
            {
                Paragraph = { Align = TextAlign.Left, Indent = 0, SpacingAfter = 0, AllowOrphanLines = false }
            };

            var fontSize = 7f;
            if (_dataSource.MaterialIssuesOverview == null || _dataSource.MaterialIssuesOverview.Count == 0) return 0;

            var columns = new[] { 0.0f, 40f, 135f, 60f, 40f, 40f, 40f, 40f, 45f, 50f, 55f };

            var tableWidth = _report.PageMaxWidth + 5;
            var tableHeight = _report.PageMaxHeight - top;

            var table = new Table(left, top, tableWidth + 15f, tableHeight);
            table.Border.Width = 0.1f;
            table.Border.Color = RgbColor.LightGrey;
            table.Border.LineStyle = LineStyle.None;
            table.BackgroundColor = RgbColor.White;
            table.CellSpacing = 0.0f;
            table.CellDefault.Align = TextAlign.Center;
            table.CellDefault.VAlign = VAlign.Center;
            table.CellDefault.Padding.Value = 0.0f;
            table.CellDefault.Border.Width = 0.2f;
            table.CellDefault.Border.LineStyle = LineStyle.None;
            table.CellDefault.Border.Color = RgbColor.WhiteSmoke;

            table.Columns.Add(columns[1]);
            table.Columns.Add(columns[2]);
            table.Columns.Add(columns[3]);
            table.Columns.Add(columns[4]);
            table.Columns.Add(columns[5]);
            table.Columns.Add(columns[6]);
            table.Columns.Add(columns[7]);
            table.Columns.Add(columns[8]);
            table.Columns.Add(columns[9]);
            table.Columns.Add(columns[10]);


            Cell cell;

            var row = table.Rows.Add(firstRowHeight);
            currentYOffset += row.ActualRowHeight;
            cell = row.Cells.Add(string.Empty);
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(string.Empty);
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(new Label("Contribution to", 0.0f, 0.0f, columns[3], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.LineStyle = LineStyle.None;

            row = table.Rows.Add(firstRowHeight);
            cell = row.Cells.Add(string.Empty);
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(string.Empty);
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(new Label("ESG Risk", 0.0f, 0.0f, columns[3], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(new Label("Subindustry", 0.0f, 0.0f, columns[4], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(new Label("Company", 0.0f, 0.0f, columns[5], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(new Label("Excess", 0.0f, 0.0f, columns[6], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(new Label("Manageable", 0.0f, 0.0f, columns[7], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(new Label("Management", 0.0f, 0.0f, columns[8], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(new Label("ESG Risk", 0.0f, 0.0f, columns[9], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.LineStyle = LineStyle.None;

            cell = row.Cells.Add(new Label("Risk", 0.0f, 0.0f, columns[10], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.LineStyle = LineStyle.None;

            row = table.Rows.Add(secondRowHeight);
            currentYOffset += row.ActualRowHeight;

            cell = row.Cells.Add(string.Empty);
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell = row.Cells.Add(new Label("Issue Name", 2.0f, 0.0f, columns[2], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Left, RgbColor.Black));
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell = row.Cells.Add(new Label("Rating", 0.0f, 2.0f, columns[3], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.VAlign = VAlign.Top;
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell = row.Cells.Add(new Label("Exposure", 0.0f, 2.0f, columns[4], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.VAlign = VAlign.Top;
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell = row.Cells.Add(new Label("Exposure", 0.0f, 2.0f, columns[5], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.VAlign = VAlign.Top;
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell = row.Cells.Add(new Label("Exposure", 0.0f, 2.0f, columns[6], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.VAlign = VAlign.Top;
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell = row.Cells.Add(new Label("Risk Factor", 0.0f, 2.0f, columns[7], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.VAlign = VAlign.Top;
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell = row.Cells.Add(new Label("Score", 0.0f, 2.0f, columns[8], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.VAlign = VAlign.Top;
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell = row.Cells.Add(new Label("Rating", 0.0f, 2.0f, columns[9], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.VAlign = VAlign.Top;
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell = row.Cells.Add(new Label("Category", 0.0f, 0.0f, columns[10], headerFontSize, Fonts.RobotoBold, headerFontSize, TextAlign.Center, RgbColor.Black));
            cell.VAlign = VAlign.Top;
            cell.Border.Bottom.LineStyle = LineStyle.Solid;
            cell.Border.Bottom.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            var nrRows = 0;
            foreach (var mei in _dataSource.MaterialIssuesOverview.OrderByDescending(x => ((MaterialIssueType)x.Type) == MaterialIssueType.BaselineIssue).ThenByDescending(x => x.FinalWeight))
            {

                // TO DO : Corporate Governance ALWAYS first.
                var hasEventIndicators = false;
                if (mei.EventIndicators.Any())
                {
                    hasEventIndicators = true;
                }

                var height = rowHeight;
                if (hasEventIndicators)
                    height = rowHeight - 4f;

                row = table.Rows.Add(height);
                currentYOffset += row.ActualRowHeight;
                nrRows++;
                //var meiName = string.Format("<p>{0}</p>", mei.Name);
                var meiName = string.Format("{0}", mei.Name);

                var formattedTextArea = new FormattedTextArea(meiName, 0, -10, columns[2] - 3f, 7f, style);

                if (hasEventIndicators)
                {
                    var maxAnswerCategory = mei.EventIndicators.Max(ei => ei.AnswerCategory);
                    var image = ReportHelper.GetEventImage(maxAnswerCategory);
                    cell = row.Cells.Add(new Image(ImageData.GetImage(image), 10.0f, 0.0f, 0.1f) { Align = Align.Left, VAlign = VAlign.Top });
                    cell.VAlign = VAlign.Center;
                    cell.Border.Left.LineStyle = LineStyle.None;
                    cell.Border.Right.LineStyle = LineStyle.None;
                    cell.Border.Bottom.LineStyle = LineStyle.Solid;
                    cell.Border.Bottom.Width = 0.5f;
                    cell.Border.Color = RgbColor.LightGrey;

                }
                else
                {
                    cell = row.Cells.Add(string.Empty);
                    cell.Border.Left.LineStyle = LineStyle.None;
                    cell.Border.Right.LineStyle = LineStyle.None;
                    cell.Border.Bottom.LineStyle = LineStyle.Solid;
                    cell.Border.Bottom.Width = 0.5f;
                    cell.Border.Color = RgbColor.LightGrey;
                }

                cell = row.Cells.Add(formattedTextArea);

                cell.VAlign = VAlign.Bottom;
                cell.Border.Left.LineStyle = LineStyle.None;
                cell.Border.Right.LineStyle = LineStyle.None;
                cell.Border.Bottom.LineStyle = LineStyle.Solid;
                cell.Border.Bottom.Width = 0.5f;
                cell.Border.Color = RgbColor.LightGrey;


                cell = row.Cells.Add((mei.FinalWeight * 100).ToString("#0.0") + "%");
                cell.VAlign = VAlign.Center;
                cell.Border.Right.LineStyle = LineStyle.None;
                cell.Border.Left.LineStyle = LineStyle.None;
                cell.Border.Bottom.LineStyle = LineStyle.Solid;
                cell.Border.Bottom.Width = 0.5f;
                cell.Border.Color = RgbColor.LightGrey;
                cell.FontSize = fontSize;
                cell.Font = Fonts.RobotoRegular;

                cell = row.Cells.Add((mei.SubIndustryExposure).ToString("#0.0"));
                cell.Border.Left.LineStyle = LineStyle.None;
                cell.Border.Right.LineStyle = LineStyle.None;
                cell.Border.Bottom.LineStyle = LineStyle.Solid;
                cell.Border.Bottom.Width = 0.5f;
                cell.Border.Color = RgbColor.LightGrey;
                cell.FontSize = fontSize;
                cell.Font = Fonts.RobotoRegular;

                cell = row.Cells.Add((mei.Exposure).ToString("#0.0"));
                cell.Border.Left.LineStyle = LineStyle.None;
                cell.Border.Right.LineStyle = LineStyle.None;
                cell.Border.Bottom.LineStyle = LineStyle.Solid;
                cell.Border.Bottom.Width = 0.5f;
                cell.Border.Color = RgbColor.LightGrey;
                cell.FontSize = fontSize;
                cell.Font = Fonts.RobotoRegular;


                if ((mei.SubIndustryExposure - mei.Exposure).ToString("#0.0") == "0.0")
                {
                    cell = row.Cells.Add("-");
                }
                else
                {
                    cell = row.Cells.Add(mei.ExcessExposure.ToString("#0.0"));
                }

                cell.Border.Left.LineStyle = LineStyle.None;
                cell.Border.Right.LineStyle = LineStyle.None;
                cell.Border.Bottom.LineStyle = LineStyle.Solid;
                cell.Border.Bottom.Width = 0.5f;
                cell.Border.Color = RgbColor.LightGrey;
                cell.FontSize = fontSize;
                cell.Font = Fonts.RobotoRegular;

                cell = row.Cells.Add((mei.ManageableRiskFactorPercentile * 100).ToString("#0") + "%");
                cell.Border.Left.LineStyle = LineStyle.None;
                cell.Border.Right.LineStyle = LineStyle.None;
                cell.Border.Bottom.LineStyle = LineStyle.Solid;
                cell.Border.Bottom.Width = 0.5f;
                cell.Border.Color = RgbColor.LightGrey;
                cell.FontSize = fontSize;
                cell.Font = Fonts.RobotoRegular;

                cell = row.Cells.Add((mei.Management).ToString("#0.0"));
                cell.Font = Fonts.RobotoRegular;
                cell.Border.Left.LineStyle = LineStyle.None;
                cell.Border.Right.LineStyle = LineStyle.None;
                cell.Border.Bottom.LineStyle = LineStyle.Solid;
                cell.Border.Bottom.Width = 0.5f;
                cell.Border.Color = RgbColor.LightGrey;
                cell.FontSize = fontSize;

                cell = row.Cells.Add(mei.UnmanagedRisk.ToString("#0.0"));
                cell.Font = Fonts.RobotoRegular;
                cell.Border.Left.LineStyle = LineStyle.None;
                cell.Border.Right.LineStyle = LineStyle.None;
                cell.Border.Bottom.LineStyle = LineStyle.Solid;
                cell.Border.Bottom.Width = 0.5f;
                cell.Border.Color = RgbColor.LightGrey;
                cell.FontSize = fontSize;

                RiskRating_CustomMiniBulletChart chartObj = new RiskRating_CustomMiniBulletChart();
                chartObj.Top = 2f;
                chartObj.Left = left - 5f;
                chartObj.Width = 20f;
                chartObj.Assessment = mei.Assessment.ToString();
                cell.VAlign = VAlign.Center;
                cell = row.Cells.Add(chartObj);
                cell.Border.Left.LineStyle = LineStyle.None;
                cell.Border.Right.LineStyle = LineStyle.None;
                cell.Border.Bottom.LineStyle = LineStyle.Solid;
                cell.Border.Bottom.Width = 0.5f;
                cell.Border.Color = RgbColor.LightGrey;

            }


            row = table.Rows.Add(totalRowHeight);

            cell = row.Cells.Add(string.Empty);
            cell.Border.LineStyle = LineStyle.None;
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell.Font = Fonts.RobotoRegular;
            cell = row.Cells.Add(new Label("Overall",2.0f, 0.0f, columns[2], fontSize, Fonts.RobotoBold, fontSize, TextAlign.Left, RgbColor.Black));
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Color = RgbColor.Black;

     
            cell.Border.Left.LineStyle = LineStyle.None;
            cell.Border.Right.LineStyle = LineStyle.None;
            cell = row.Cells.Add(new Label("100.0%", 0.0f, 0.0f, columns[3], fontSize, Fonts.RobotoBold, fontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell.Border.Left.LineStyle = LineStyle.None;
            cell.Border.Right.LineStyle = LineStyle.None;
            cell = row.Cells.Add(new Label(_dataSource.CompanyProfile.SubIndustryExposureScore.Value.ToString("#0.0"), 0.0f, 0.0f, columns[4], fontSize, Fonts.RobotoBold, fontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell.Border.Left.LineStyle = LineStyle.None;
            cell.Border.Right.LineStyle = LineStyle.None;
            cell = row.Cells.Add(new Label(_dataSource.CompanyProfile.ManagementRisk.Exposure.Value.ToString("#0.0"),
                0.0f, 0.0f, columns[5], fontSize, Fonts.RobotoBold, fontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell.Border.Left.LineStyle = LineStyle.None;
            cell.Border.Right.LineStyle = LineStyle.None;
            cell = row.Cells.Add(new Label(_dataSource.CompanyProfile.OverallExcessExposure.Value.ToString("#0.0"), 0.0f, 0.0f, columns[6], fontSize, Fonts.RobotoBold, fontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell.Border.Left.LineStyle = LineStyle.None;
            cell.Border.Right.LineStyle = LineStyle.None;
            cell = row.Cells.Add(new Label("-", 2.0f, 0.0f, columns[7], fontSize, Fonts.RobotoBold, fontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell.Border.Left.LineStyle = LineStyle.None;
            cell.Border.Right.LineStyle = LineStyle.None;
            cell = row.Cells.Add(new Label(_dataSource.CompanyProfile.ManagementRisk.Management.Value.ToString("#0.0"),
                2.0f, 0.0f, columns[4], fontSize + 2, Fonts.RobotoBold, fontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell.Border.Left.LineStyle = LineStyle.None;
            cell.Border.Right.LineStyle = LineStyle.None;
            cell = row.Cells.Add(new Label(_dataSource.CompanyProfile.ManagementRisk.UnmanagedRisk.Value.ToString("#0.0"),
                2.0f, 0.0f, columns[4], fontSize + 2, Fonts.RobotoBold, fontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.Top.LineStyle = LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            cell.Border.Left.LineStyle = LineStyle.None;
            cell.Border.Right.LineStyle = LineStyle.None;
            cell = row.Cells.Add(new Label(_dataSource.CompanyProfile.Assessment.ToString(),
                2.0f, 0.0f, columns[4], fontSize + 2, Fonts.RobotoBold, fontSize, TextAlign.Center, RgbColor.Black));
            cell.Border.Top.LineStyle= LineStyle.Solid;
            cell.Border.Top.Width = 1f;
            cell.Border.Color = RgbColor.Black;

            page.Elements.Add(table);

            var elements = new Group();
            var startingPos = 10f;
            currentYOffset = (nrRows + 1) * rowHeight + 150f;

            elements.Add(new Line(startingPos, currentYOffset - 2f, startingPos - 4f, currentYOffset - 8f, 0.5f, Colors.LineColor));
            elements.Add(new Line(startingPos - 4f, currentYOffset - 8f, startingPos - 8f, currentYOffset - 2f, 0.5f, Colors.LineColor));
            elements.Add(new Line(startingPos - 8f, currentYOffset - 2f, startingPos, currentYOffset - 2f, 0.5f, Colors.LineColor));
            elements.Add(new Label(" =Significant event", startingPos + 5f, currentYOffset - 10f, 150f, 9f, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));

            page.Elements.Add(elements);
            currentYOffset += 10f;
            return currentYOffset;
        }

        private void AddRiskDetailsChart(Page page, float top)
        {
            var currentYOffset = top + 15f;
            var columnWidth = (page.Dimensions.Width- (float)HorisontalSpacing.Large) / 3;
            var risk_analysis_image = ReportHelper.GetFile("icon-risk-analysis.png");
            var nameSection = "Risk Details";
            page.Elements.Add(new Image(ImageData.GetImage(risk_analysis_image), 0, currentYOffset - 13f, 0.25f));
            page.Elements.Add(new Label(nameSection, 10f, currentYOffset - 7f, 100f, 12f, Fonts.MontserratBold, 10f, TextAlign.Center, RgbColor.Black));
            page.Elements.Add(new Line(100, currentYOffset, _report.PageMaxWidth, currentYOffset, 1, RgbColor.Black));

            currentYOffset += 50f;

            RiskRating_RiskRangeBarChart chartObj = new RiskRating_RiskRangeBarChart();
            chartObj.Left = 0f;
            chartObj.Top = currentYOffset;
            chartObj.Width = columnWidth;
            chartObj.Value1 = _dataSource.CompanyProfile.ManagementRisk.Exposure.Value;
            chartObj.Value2 = _dataSource.CompanyProfile.ManagementRisk.ManageableRisk.Value;
            chartObj.Value3 = _dataSource.CompanyProfile.ManagementRisk.ManagedRisk.Value;
            chartObj.Value4 = _dataSource.CompanyProfile.ManagementRisk.ManagementGap.Value;
            chartObj.Value5 = _dataSource.CompanyProfile.ManagementRisk.UnmanageableRisk.Value;
            chartObj.Value6 = _dataSource.CompanyProfile.ManagementRisk.UnmanagedRisk.Value;
            chartObj.BarHeight = 10f;
            chartObj.ChartType = RiskRangeChartType.OverallRiskDetails;

            page.Elements.Add(chartObj);
           
        }

        private float AddIndicatorTable(CoreRiskRatingResearchProfile coreCompanyProfile, PdfReportPage page, float x, float y, float maxWidth, float maxHeight)
        {
            PdfReportPage page2 = null;
            float currentYOffset = y + 40f;
            x += 10f;

            var management_details_image = ReportHelper.GetFile("icon-management.png");
            page.Elements.Add(new Image(ImageData.GetImage(management_details_image), 0, y + 3f, 0.3f));
            page.Elements.Add(new Label("Management Overview", 20f, y + 10f, 135f, 12f, Fonts.MontserratBold, 10f, TextAlign.Center, RgbColor.Black));
            page.Elements.Add(new Line(155f, y + 15f, _report.PageMaxWidth, y + 15f, 1, RgbColor.Black));

            Table indicatorsTable = PdfReportSectionGenerator.GetDefaultTable(x, currentYOffset, maxWidth, maxHeight);

            indicatorsTable.CellDefault.VAlign = VAlign.Center;

            indicatorsTable.Columns.Add(maxWidth * 0.05f);
            indicatorsTable.Columns.Add(maxWidth * 0.50f);
            indicatorsTable.Columns.Add(maxWidth * 0.15f);
            indicatorsTable.Columns.Add(maxWidth * 0.15f);
            indicatorsTable.Columns.Add(maxWidth * 0.15f);

            var row = indicatorsTable.Rows.Add(20);
            row.Cells.Add(new Label(String.Empty, 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));
            row.Cells.Add(new Label("Indicators", 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoBold, 7f, TextAlign.Left, RgbColor.Black));
            row.Cells.Add(new Label("Raw score", 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoBold, 7f, TextAlign.Center, RgbColor.Black));
            row.Cells.Add(new Label("Weight", 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoBold, 7f, TextAlign.Center, RgbColor.Black));
            row.Cells.Add(new Label("Weighted Score", 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoBold, 7f, TextAlign.Center, RgbColor.Black));
            row.CellDefault.Border.Bottom.LineStyle = LineStyle.Solid;
            row.CellDefault.Border.Bottom.Width = 1f;
            row.CellDefault.Border.Color = RgbColor.Black;
            page.Elements.Add(indicatorsTable);

            currentYOffset += row.ActualRowHeight;

            var indicators = coreCompanyProfile.Indicators
                .Where(i => i.IndicatorType == IndicatorType.StandardIndicator)
                .OrderBy(i => i.Code, _indicatorComparer)
                .ToList();

            double totalWeightedScore = 0;

            foreach (var indicator in indicators)
            {
                row = indicatorsTable.Rows.Add(15);
                row.Cells.Add(new Label(String.Empty, 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));
                row.Cells.Add(new Label(indicator.Name ?? "Indicator name placeholder", 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));

                if (indicator.Name != null)
                {
                    PageElementPositions.Add(new PageElementPosition
                    {
                        Name = indicator.Name,
                        PageNumber = page.PageNumber,
                        HorizontalPosition = x,
                        VerticalPosition = currentYOffset,
                        Width = maxWidth * 0.50f,
                        Height = row.ActualRowHeight,
                    });
                }

                row.Cells.Add(new Label(indicator.RawScore.Value.ToString("0"), 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
                row.Cells.Add(new Label(indicator.FinalWeight.Value.ToString("0.0%"), 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
                row.Cells.Add(new Label(indicator.WeightedScore.Value.ToString("0.0"), 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
                row.CellDefault.Border.Bottom.LineStyle = LineStyle.Solid;
                row.CellDefault.Border.Bottom.Width = 0.25f;
                row.CellDefault.Border.Color = new RgbColor(112, 112, 112);
                currentYOffset += row.ActualRowHeight;
                totalWeightedScore += indicator.WeightedScore.Value;

                if (currentYOffset + 60 > maxHeight)
                {
                    currentYOffset = y + 40f;
                    indicatorsTable = ExpandTableToNextPage(x, currentYOffset, maxWidth, maxHeight, out page2);
                }

            }
            var eventsSortedList = coreCompanyProfile.Indicators.Where(a => a.IndicatorType == IndicatorType.EventIndicator).OrderByDescending(a => a.AnswerCategory.Value).ThenBy(a => a.Name);
            foreach (var indicator in eventsSortedList)
            {
                row = indicatorsTable.Rows.Add(15);
                row.Cells.Add(new Image(ImageData.GetImage(ReportHelper.GetEventImage(indicator.AnswerCategory.Value)), 0, 0, 0.1f));
                row.Cells.Add(new Label(indicator.Name ?? "Event name placeholder", 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));
                var category = indicator.AnswerCategoryText?.Value ?? "Category " + indicator.AnswerCategory.Value;
                if (category == "No evidence of relevant controversies")
                {
                    category = "Category 0";
                }
                row.Cells.Add(new Label(category, 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
                row.Cells.Add(new Label(indicator.FinalWeight.Value.ToString("0.0%"), 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
                row.Cells.Add(new Label(indicator.WeightedScore.Value.ToString("0.0"), 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
                row.CellDefault.Border.Bottom.LineStyle = LineStyle.Solid;
                row.CellDefault.Border.Bottom.Width = 0.25f;
                row.CellDefault.Border.Color = new RgbColor(112, 112, 112);
                currentYOffset += row.ActualRowHeight;

                if (currentYOffset + 60 > maxHeight)
                {
                    currentYOffset = y + 40f;
                    indicatorsTable = ExpandTableToNextPage(x, currentYOffset, maxWidth, maxHeight, out page2);
                }
            }

            double constant = _dataSource.CoreCompanyProfile.IndentPercentile.Value;
            row = indicatorsTable.Rows.Add(15);
            row.Cells.Add(new Label(String.Empty, 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));
            row.Cells.Add(new Label("Constant*", 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));
            row.Cells.Add(new Label("100", 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
            row.Cells.Add(new Label(constant.ToString("0.0%"), 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
            row.Cells.Add(new Label((constant * 100).ToString("0.0"), 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
            row.CellDefault.Border.Bottom.LineStyle = LineStyle.Solid;
            row.CellDefault.Border.Bottom.Width = 0.25f;
            row.CellDefault.Border.Color = new RgbColor(112, 112, 112);
            currentYOffset += row.ActualRowHeight;
            totalWeightedScore += (constant * 100);

            row = indicatorsTable.Rows.Add(20);
            row.Cells.Add(new Label(String.Empty, 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));
            row.Cells.Add(new Label("Totals", 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoBold, 7f, TextAlign.Left, RgbColor.Black));
            row.Cells.Add(new Label(String.Empty, 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
            row.Cells.Add(new Label("100.0%", 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoBold, 7f, TextAlign.Center, RgbColor.Black));
            row.Cells.Add(new Label(totalWeightedScore.ToString("0.0"), 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoBold, 7f, TextAlign.Center, RgbColor.Black));
            currentYOffset += 30;

            

            if (page2 != null)
            {
                page = page2;
                currentYOffset += 30;
            }

            var startingPos = 17f;
            page.Elements.Add(new Line(startingPos, currentYOffset - 2f, startingPos - 4f, currentYOffset - 8f, 0.5f, RgbColor.Black));
            page.Elements.Add(new Line(startingPos - 4f, currentYOffset - 8f, startingPos - 8f, currentYOffset - 2f, 0.5f, RgbColor.Black));
            page.Elements.Add(new Line(startingPos - 8f, currentYOffset - 2f, startingPos, currentYOffset - 2f, 0.5f, RgbColor.Black));
            page.Elements.Add(new Label("= Event indicator", startingPos + 3f, currentYOffset - 8f, 150f, 9f, Fonts.RobotoRegular, 6f, TextAlign.Left, RgbColor.Black));
            currentYOffset += 15;

            string footnote = "* Sustainalytics measures management performance on a focused set of indicators for core companies and applies a constant value to the management score to account for the contribution to management score that would be expected from indicators that are not in use.";
            page.Elements.Add(new TextArea(footnote, x, currentYOffset, maxWidth, 50, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));
            return currentYOffset;
        }

        private Table ExpandTableToNextPage(float x, float y, float maxWidth, float maxHeight, out PdfReportPage page2)
        {
            page2 = new PdfReportPage_Core(_report, _dataSource, 2);
            _report.Pages.Add(page2);
            OutlinePageCount++;
            var currentYOffset = y;

            Table indicatorsTable = PdfReportSectionGenerator.GetDefaultTable(x, currentYOffset, maxWidth, maxHeight);

            indicatorsTable.CellDefault.VAlign = VAlign.Center;
            indicatorsTable.Columns.Add(maxWidth * 0.05f);
            indicatorsTable.Columns.Add(maxWidth * 0.50f);
            indicatorsTable.Columns.Add(maxWidth * 0.15f);
            indicatorsTable.Columns.Add(maxWidth * 0.15f);
            indicatorsTable.Columns.Add(maxWidth * 0.15f);

            var row = indicatorsTable.Rows.Add(20);
            row.Cells.Add(new Label(String.Empty, 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));
            row.Cells.Add(new Label("Indicators", 0.0f, 0.0f, maxWidth * 0.50f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Left, RgbColor.Black));
            row.Cells.Add(new Label("Raw score", 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
            row.Cells.Add(new Label("Weight", 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
            row.Cells.Add(new Label("Weighted Score", 0.0f, 0.0f, maxWidth * 0.15f, 7f, Fonts.RobotoRegular, 7f, TextAlign.Center, RgbColor.Black));
            row.CellDefault.Border.Bottom.LineStyle = LineStyle.Solid;
            row.CellDefault.Border.Bottom.Width = 1f;
            row.CellDefault.Border.Color = RgbColor.Black;
            page2.Elements.Add(indicatorsTable);

            return indicatorsTable;
        }
    }

}
